package com.jzo2o.customer.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jzo2o.customer.mapper.BankAccountMapper;
import com.jzo2o.customer.model.domain.BankAccount;
import com.jzo2o.customer.model.dto.request.BankAccountUpsertReqDTO;
import com.jzo2o.customer.model.dto.response.BankAccountResDTO;
import com.jzo2o.customer.service.IBankAccountService;
import com.jzo2o.mvc.utils.UserContext;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

/**
 * @author 赵超
 */
@Service
@Slf4j
public class BankAccountServiceImpl extends ServiceImpl<BankAccountMapper, BankAccount> implements IBankAccountService {
    @Override
    public void addBankAccount(BankAccountUpsertReqDTO dto) {
        BankAccount bankAccount = BeanUtil.toBean(dto, BankAccount.class);
        Long userId = UserContext.currentUserId();
        bankAccount.setId(userId);
        BankAccount account = super.getById(userId);
        if (ObjectUtil.isNull(account)) {
            bankAccount.setCreateTime(LocalDateTime.now());
            bankAccount.setUpdateTime(LocalDateTime.now());
            super.save(bankAccount);
        } else {
            bankAccount.setUpdateTime(LocalDateTime.now());
            super.updateById(bankAccount);
        }


    }

    @Override
    public BankAccountResDTO currentUserBankAccount() {
        Long userId = UserContext.currentUserId();
        BankAccount bankAccount = super.getById(userId);
        return BeanUtil.toBean(bankAccount, BankAccountResDTO.class);
    }
}
